const qiniu = require("qiniu")
const accessKey = 'mJ6qZUvu_-0lfUIXCJR72IXbcb3dPOIFK4T3oTLh';
const secretKey = 'YDVkKKUna6tNFqQZbNJmr7fy9gU-5qWc7OSRawAO';
const bucket = "shaliang";
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

const options = {
  scope: bucket
}
const putPolicy = new qiniu.rs.PutPolicy(options);
const uploadToken = putPolicy.uploadToken(mac);
const config = new qiniu.conf.Config();
config.useCdnDomain = true;
// 空间对应的机房
config.zone = qiniu.zone.Zone_z0;
const formUploader = new qiniu.form_up.FormUploader(config);
const putExtra = new qiniu.form_up.PutExtra();

// 文件上传
module.exports = function (key, path) {
  return new Promise(function (resolve, reject) {
    formUploader.putFile(uploadToken, key, path, putExtra, function (respErr, respBody, respInfo) {
      console.error(respBody);
      console.error(respErr);
      if (respErr) {
        reject(respErr);
      }
      if (respInfo !== null && (respInfo.statusCode == 200)) {
        resolve(respBody);
      } else {
        reject(respInfo&&respInfo.statusCode) ;
        console.error(respBody);
      }
    });
  })
};

